package com.xucranger.ojapi.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xucranger.ojapi.enity.DTO.UserinfoDTO;
import com.xucranger.ojapi.enity.DTO.common.ResponseCodeConst;
import com.xucranger.ojapi.enity.DTO.common.ResponseDTO;
import com.xucranger.ojapi.enity.VO.UserinfoVO;
import com.xucranger.ojapi.enity.domain.Contests;
import com.xucranger.ojapi.enity.domain.RegisterUser;
import com.xucranger.ojapi.enity.domain.Student;
import com.xucranger.ojapi.enity.domain.Userinfo;
import com.xucranger.ojapi.mapper.StudentMapper;
import com.xucranger.ojapi.mapper.UserinfoMapper;
import com.xucranger.ojapi.service.AtcoderService;
import com.xucranger.ojapi.service.CodeforcesService;
import com.xucranger.ojapi.service.CommonService;
import com.xucranger.ojapi.service.UserinfoService;

import com.xucranger.ojapi.utils.SmartBeanUtil;
import com.xucranger.ojapi.utils.SmartDigestUtil;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
* @author xucranger
* @description 针对表【userinfo】的数据库操作Service实现
* @createDate 2022-08-26 09:01:54
*/
@Service
public class UserinfoServiceImpl extends ServiceImpl<UserinfoMapper, Userinfo>
implements UserinfoService {

    @Autowired
    UserinfoMapper userinfoMapper;
    @Autowired
    StudentMapper studentMapper;
    @Autowired
    AtcoderService atcoderService;
    @Autowired
    CodeforcesService codeforcesService;
    @Autowired
    CommonService commonService;



    //注册
    @Override
    public ResponseDTO register(RegisterUser registerUser) {
        registerUser.setStatus("user");
        registerUser.setUserpsd(SmartDigestUtil.encryptPassword(registerUser.getUserpsd()));
        QueryWrapper querystudent = new QueryWrapper<>();
        QueryWrapper queryuserinfo = new QueryWrapper<>();
        querystudent.eq("stuid",registerUser.getUserid());
        queryuserinfo.eq("userid",registerUser.getUserid());

        Userinfo userinfo = SmartBeanUtil.copy(registerUser, Userinfo.class);
        Student student = SmartBeanUtil.copy(registerUser,Student.class);


       if(userinfoMapper.selectCount(queryuserinfo)!=0){
            //id已经被注册
            return ResponseDTO.wrap(ResponseCodeConst.ID_HAS_EXIST);
        }
        try {
            userinfoMapper.insert(userinfo);
            studentMapper.insert(student);
        }
        catch (Exception exp) {
            log.error("注册发生错误:"+exp.getMessage());
            return ResponseDTO.wrap(ResponseCodeConst.ERROR_PARAM);
        }
        return ResponseDTO.succ();
    }

    @Override
    public ResponseDTO<UserinfoDTO> login(Userinfo userinfo) {
        //密码加密
        String password = SmartDigestUtil.encryptPassword(userinfo.getUserpsd());
        UserinfoDTO userinfoDTO = userinfoMapper.login(userinfo.getUserid(),password);
        if(userinfoDTO==null){
            return ResponseDTO.wrap(ResponseCodeConst.PSWD_ERROR);
        }
        return ResponseDTO.succData(userinfoDTO);
    }

    @Override
    public ResponseDTO<UserinfoVO> getPofile(String userid) {


        UserinfoVO  userinfoVO  = new UserinfoVO();


        //获取个人信息
        Student student = studentMapper.selectById(userid);
        userinfoVO.setStudent(student);


        //获取生涯
        userinfoVO.setAccaree(atcoderService.queryCareeByAcid(student.getStuAcid()).getData());
        userinfoVO.setCfcaree(codeforcesService.queryCareeByCfid(student.getStuCfid()).getData());
        if(!student.getStuCfid1().isEmpty()){
            userinfoVO.setCf1caree(codeforcesService.queryCareeByCfid(student.getStuCfid1()).getData());
        }
        if(!student.getStuCfid2().isEmpty()){
            userinfoVO.setCf2caree(codeforcesService.queryCareeByCfid(student.getStuCfid2()).getData());
        }
        //获取最近比赛记录
        userinfoVO.setLatestcontests(commonService.getcontestsbyid(1,10,student.getStuid()).getData());


        return ResponseDTO.succData(userinfoVO);
    }

}
